package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;

import java.util.ArrayList;

//红外学习界面
public class HongWStudyActivity extends Activity implements OnClickListener {


    private String s;
    private MyApplication m;
    private String s1;
    Dialog dialog;
    public int id;
    public String selfLearning;
    public ArrayList button;
    public ArrayList code;
    public String name;


    Handler hand = new Handler() {

        public void handleMessage(android.os.Message msg) {

            if (msg.what == 1 && dialog != null) {
                dialog.dismiss();
                Intent it = new Intent(HongWStudyActivity.this, MaKulistActivity.class);
                startActivity(it);
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hong_wstudy);
    }

    @Override
    protected void onStart() {
        info();
        super.onStart();
    }

    private void info() {
        m = (MyApplication) getApplication();
        m.hand = hand;
        Intent it = getIntent();
        s = it.getStringExtra("flag");
        id = it.getIntExtra("id", -1);
        selfLearning = it.getStringExtra("selfLearning");
        button = it.getStringArrayListExtra("button");
        code = it.getStringArrayListExtra("code");
        name = it.getStringExtra("name");
        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_hongwstudy);

        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });

        Button bt = (Button) findViewById(R.id.xstudy);// 选择型号
        Button bt1 = (Button) findViewById(R.id.astudy);// 按键学习
        Button bt2 = (Button) findViewById(R.id.ystudy);// 一键学习
        bt.setOnClickListener(this);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.xstudy:
                if ("1".equals(s)) {
                    Intent it = new Intent(HongWStudyActivity.this, SbXingHaoActivity.class);
                    if (id != -1) {
                        it.putExtra("id", id);
                    }
                    startActivity(it);
                } else {
                    ToastUtils.showToast(getBaseContext(),"该类设备无法通过选择品牌设置遥控器");
                }

                break;


            case R.id.astudy:
                if ("04".equals(m.type)) {
                    Intent it = new Intent(HongWStudyActivity.this, StudyYaoKongQiActivity.class);
                    it.putExtra("flag", "a");
                    if (id != -1) {
                        it.putExtra("id", id);
                        it.putStringArrayListExtra("button", button);
                        it.putStringArrayListExtra("code", code);
                        it.putExtra("name", name);
                    }
                    startActivity(it);

                } else if ("02".equals(m.type)) {
                    Intent it = new Intent(HongWStudyActivity.this, StudyjdhActivity.class);
                    it.putExtra("flag", "a");
                    if (id != -1) {
                        it.putExtra("id", id);
                        it.putStringArrayListExtra("button", button);
                        it.putStringArrayListExtra("code", code);
                        it.putExtra("name", name);
                    }
                    startActivity(it);
                } else if ("06".equals(m.type)) {
                    ToastUtils.showToast(getBaseContext(),"该类设备无法通过按键学习设置遥控器");
                    /*
                    Intent it = new Intent(HongWStudyActivity.this, StudyKTActivity.class);
                    it.putExtra("flag", "a");
                    if (id != -1) {
                        it.putExtra("id", id);
                    }
                    startActivity(it);
                    finish();*/

                }

                break;

            case R.id.ystudy:
                SheBeiClass sb = new SheBeiClass();
                sb.ieee = m.ieee_ykq;
                sb.cmd = "1012";
                sb.port = m.port_ykq;
                sb.mac=m.mac_ykq;
                sb.extra = new Extra();
                sb.extra.type = m.type;
                sb.status = "03";
                String l = JSON.toJSONString(sb);
                 Log.d("l",l);
                if (!Gateway.MacSocke.containsKey(sb.mac)) {
                    if(m.flag3){
                    m.c.send(l);}
                }else{
                    Log.d("内网发送","内网发送");
                    String cmd= GatewayRequestUtil.createInfraredForwardingStr2(sb.ieee,sb.port,m.type,"01");
                    SendUtil.Mac = sb.mac;
                    SendUtil.cmd = cmd;
                    SendUtil.hand.sendEmptyMessage(0);
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(HongWStudyActivity.this);
                builder.setTitle("提示");
                builder.setMessage("请点击遥控器的开关键来获取码库列表");
                dialog = builder.create();
                dialog.show();
                break;
        }

    }

}
